﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Southridge.Data 
{

    /// <summary>
    /// DataEntity is an abstraction to a readonly dataobject
    /// It does not implement INPC since it never changes 
    /// Examples: City, County, CountryCodes, etc.. it is all from the database.. 
    /// </summary>
    public class DataEntity
    {

    } 

    /// <summary>
    /// BusinessObject implementing INPC.. 
    /// </summary>
    public class BusinessEntity : DataEntity, INotifyPropertyChanged 
    {
        protected Dictionary<string, string> _errors = new Dictionary<string, string>();
        protected bool _isDirty = false;
        protected bool _isValid  = true;
        
        public virtual bool IsDirty
        {
            get
            {
                return _isDirty; 
            }
            internal set
            {
                if (_isDirty != value)
                {
                    _isDirty = value;
                    OnPropertyChanged("IsDirty"); 
                } 
            } 
        }

        public virtual bool IsValid
        {

            get
            {
                return ( _errors.Count == 0 );
            }

            internal set
            {
                if ( _isValid!= value)
                {
                    _isDirty = value;
                    OnPropertyChanged("IsValid");
                }
            } 
        } 

        public virtual void Validate () 
        {
            IsValid = true; 
        } 

        public virtual void Save ( ) 
        {
             
            
        } 


        #region INP
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            
            bool oldDirty = _isDirty;
            bool isDirtyPropertySetter = (propertyName == "IsDirty");
            if (!isDirtyPropertySetter)
            {             
                _isDirty = true;
            }

            PropertyChangedEventHandler ph = PropertyChanged;
            if (ph != null)
            {
                ph(this, new PropertyChangedEventArgs(propertyName));
                if (_isDirty != oldDirty)
                    ph(this, new PropertyChangedEventArgs("IsDirty"));
                else if ( isDirtyPropertySetter ) 
                    ph(this, new PropertyChangedEventArgs("IsDirty"));
            } 

            Validate(); 

        }

        
        #endregion 
    }

    
       
   
    public abstract class Property  : INotifyPropertyChanged 
    {
        
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler ph = PropertyChanged; 
            if ( ph != null ) 
                ph ( this, new PropertyChangedEventArgs (propertyName )); 
        } 
        #endregion
    }


    
 
}
